<?php

class Ead_IndexController extends Ead_Controller_Action
{

    public function init()
    {
        
    }

    public function indexAction()
    {   
        Zend_Loader::loadClass('calendario','library');
        Zend_Loader::loadClass('datetools','library');
        Zend_Loader::loadClass('dominio','library');
        
        
        // link uteis
        $query = 'SELECT SQL_CACHE * FROM LINKUTEIS  ORDER BY LNK_ORD ASC LIMIT 2';
        $this->view->linkuteis = $this->db->fetchAll($query);
        
        $this->view->mural = array();
        $this->view->horarios = array();
        $this->view->dia = array();
        $this->view->ves = array();
        
        // aluno
        if($this->storage->read()->TUS_ID == 2){
            
            $sessao = new Zend_Session_Namespace('Aluno');
            
            $s_turma = new Zend_Session_Namespace('Turma');
            
            if($s_turma->turma['CUR_ID'] == 0)
            {
                $query = 'SELECT SQL_CACHE m.MAT_ID, c.* '
                .' FROM MATRICULA m, CURSOBASE cb, CURSO c, NUMEROTURMA n '
                .' WHERE m.ALU_ID = '.$sessao->aluno['ALU_ID'].' '
                .' AND m.CUR_ID = c.CUR_ID AND c.CBS_ID = cb.CBS_ID AND c.CUR_TRM = n.NRT_ID '
                .' AND m.MAT_STA = 1';
                $row = $this->db->fetchRow($query);
                
                $s_turma->turma = $row;
            } 
            
            $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/ead/index.js');
            
            // mural
            //$where = ' AND m.ALU_ID = 0';
            $where = ' AND ms.ALU_ID = '.$sessao->aluno['ALU_ID']; // msg enviado para aluno

            $query = 'SELECT SQL_CACHE ms.*, u.USU_NOME FROM MURAL_RECADOS_MSG ms, MURAL_RECADOS m, USUARIOS u WHERE ms.MRC_ID = m.MRC_ID AND m.USU_ID = u.USU_ID '.$where.'  AND m.MRC_STA > 0 AND ms.MRM_STA > 0 ORDER BY ms.MRM_DAT DESC, ms.MRM_HOR DESC';
            $mural = $this->db->fetchAll($query); 
            $this->view->mural = $this->paginacao($mural);
            /*
            $query = '(SELECT SQL_CACHE u.USU_NOME '
                    .' FROM MURAL_RECADOS_MSG ms, MURAL_RECADOS m, USUARIOS u WHERE ms.MRC_ID = m.MRC_ID AND m.USU_ID = u.USU_ID '.$where.'  AND m.MRC_STA > 0 AND ms.MRM_STA > 0 ORDER BY ms.MRM_DAT DESC, ms.MRM_HOR DESC)';
           
            $query .= ' UNION(SELECT SQL_CACHE u.USU_NOME '
                    .' FROM EAD_CONTATO' c, ALUNO a, USUARIOS u  WHERE c.ALU_ID = a.ALU_ID AND a.USU_ID = u.USU_ID ORDER BY c.CON_DATHOR DESC)';
            
            $mural = $this->db->fetchAll($query); 
            $this->view->mural = $this->paginacao($mural);
            */

            // fim mural
            
            
            // calendario
            $ano = date("Y", mktime(0, 0, 0, date('m'), 1, date("Y")));//Pega o ano
            $cont = 0;

            $dias = array();//Array que irá guardar os dias do mês
            $mes = date("m", mktime(0, 0, 0, date('m'), 1, date('Y')));//date("m");//Pega o mês
            $dia = ($mes == date("m") && $ano == date("Y") ? date("d"):'');//Pega o dia
            $totalDias = date("t", mktime(0, 0, 0, date('m'), 1, date('Y')));;//date("t");//Pega a qtd de dias do mês
            $primeiroDia = date("D", mktime(0, 0, 0, date('m'), 1, date('Y')));//Vê em que dia da semana caiu o 1º dia do mês
            //Aqui irá adicionar os dias do mês no array
            for($d = 0; $d < $totalDias; $d++) $dias[$d] = array_push($dias, $d+1);

            $pos = Datetools::diasemanaTextoIng($primeiroDia);

            //
            $dia = array();
            $horarios = array();
            $sem = '';

            $query = 'SELECT SQL_CACHE c.CUR_ID,cb.CBS_NOM,n.* FROM MATRICULA m, CURSOBASE cb, CURSO c, NUMEROTURMA n '
                    . 'WHERE m.ALU_ID = '.$sessao->aluno['ALU_ID'].' '
                    . 'AND m.CUR_ID = c.CUR_ID AND c.CBS_ID = cb.CBS_ID AND c.CUR_TRM = n.NRT_ID '
                    . 'AND m.MAT_STA = 1';
            $hor = $this->db->fetchAll($query); 

            foreach($hor as $h){
                $sem = $h['NRT_SEM'];

                for($linha = 0; $linha < 6; $linha++) {
                    for($coluna = 0; $coluna < 7; $coluna++) {
                        $pos2 = $cont - $pos;
                        if ($dias[$pos2] < 10) $dias[$pos2] = "0" . $dias[$pos2];
                        //echo $dias[$pos2]; 

                        $w = date('w', mktime(0, 0, 0, date('m'), $dias[$pos2], date('Y')));
                        if($sem == $w && $dias[$pos2] > 0){
                            //echo $dias[$pos2];
                            $horarios[] = '<strong>'.$dias[$pos2].' às '.substr($h['NRT_HOR'],0,5).'</strong>  '.dominio::getTurma($h['CUR_ID'],0); //$h['NRT_NOM'].' - '.$h['CBS_NOM'];
                            $dia[$dias[$pos2]] = $dias[$pos2].' às '.substr($h['NRT_HOR'],0,5).'  '.dominio::getTurma($h['CUR_ID'],0); //$h['NRT_NOM'].' - '.$h['CBS_NOM'];
                        }

                        $cont++;
                    }
                }
            }

            //usort($horarios);
            $this->view->horarios = $horarios;
            $this->view->dia = $dia;
            
            $query = 'SELECT SQL_CACHE *, DAY(CAL_DAT) as DIA, MONTH(CAL_DAT) as MES FROM CALENDARIO WHERE YEAR(CAL_DAT) = "'. date('Y').'" and MONTH(CAL_DAT) = "'. date('m').'" and CAL_STA = 1 ORDER BY CAL_DAT ASC, CAL_NOM ASC LIMIT 18';
            $ves = $this->db->fetchAll($query);
            $this->view->ves = $ves;
            // fim calendario
        }
        else // professor
        if($this->storage->read()->TUS_ID == 3){
            
            $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/ead/indexprf.js?1=1');
            
            $this->_helper->viewRenderer->setRender('indexprf');  
            
            //$sessao = new Zend_Session_Namespace('Aluno');
            
            $sessao = new Zend_Session_Namespace('Professor');
            
            // mural
            
            $where = ' AND m.MRC_ID > 0 ';
            $where = ' AND m.USU_ID = '.$this->storage->read()->USU_ID; // usuário que envio o recado
            $query = 'SELECT SQL_CACHE m.*, u.USU_NOME FROM MURAL_RECADOS m, USUARIOS u WHERE m.USU_ID = u.USU_ID '.$where.'  AND m.MRC_STA > 0 ORDER BY m.MRC_DAT DESC, m.MRC_HOR DESC';
            $mural = $this->db->fetchAll($query); 
            $this->view->mural = $this->paginacao($mural);
            
            /*
            $query = '(SELECT SQL_CACHE m.MRC_ID,m.MRC_STA,m.MRC_DAT,m.MRC_HOR,m.USU_NOME '
                    .' FROM MURAL_RECADOS_MSG ms, MURAL_RECADOS m, USUARIOS u WHERE ms.MRC_ID = m.MRC_ID AND m.USU_ID = u.USU_ID '.$where.'  AND m.MRC_STA > 0 AND ms.MRM_STA > 0 ORDER BY ms.MRM_DAT DESC, ms.MRM_HOR DESC)';
           
            $where = ' AND c.PRS_ID = '.$sessao->professor['PRS_ID'];
            
            echo $query .= ' UNION(SELECT SQL_CACHE u.USU_NOME '
                    .' FROM EAD_CONTATO c, ALUNO a, USUARIOS u  WHERE c.ALU_ID = a.ALU_ID AND a.USU_ID = u.USU_ID '.$where.' ORDER BY c.CON_DATHOR DESC)';
            $mural = $this->db->fetchAll($query); 
            $this->view->mural = $this->paginacao($mural);
            */
            // fim mural
            
            /*
            // calendario
            $ano = date("Y", mktime(0, 0, 0, date('m'), 1, date("Y")));//Pega o ano
            $cont = 0;

            $dias = array();//Array que irá guardar os dias do mês
            $mes = date("m", mktime(0, 0, 0, date('m'), 1, date('Y')));//date("m");//Pega o mês
            $dia = ($mes == date("m") && $ano == date("Y") ? date("d"):'');//Pega o dia
            $totalDias = date("t", mktime(0, 0, 0, date('m'), 1, date('Y')));;//date("t");//Pega a qtd de dias do mês
            $primeiroDia = date("D", mktime(0, 0, 0, date('m'), 1, date('Y')));//Vê em que dia da semana caiu o 1º dia do mês
            //Aqui irá adicionar os dias do mês no array
            for($d = 0; $d < $totalDias; $d++) $dias[$d] = array_push($dias, $d+1);

            $pos = Datetools::diasemanaTextoIng($primeiroDia);

            //
            $dia = array();
            $horarios = array();
            $sem = '';

            $query = 'SELECT SQL_CACHE c.CUR_ID,cb.CBS_NOM,n.* FROM MATRICULA m, CURSOBASE cb, CURSO c, NUMEROTURMA n '
                    . 'WHERE m.ALU_ID = '.$sessao->aluno['ALU_ID'].' '
                    . 'AND m.CUR_ID = c.CUR_ID AND c.CBS_ID = cb.CBS_ID AND c.CUR_TRM = n.NRT_ID '
                    . 'AND m.MAT_STA = 1';
            $hor = $this->db->fetchAll($query); 

            foreach($hor as $h){
                $sem = $h['NRT_SEM'];

                for($linha = 0; $linha < 6; $linha++) {
                    for($coluna = 0; $coluna < 7; $coluna++) {
                        $pos2 = $cont - $pos;
                        if ($dias[$pos2] < 10) $dias[$pos2] = "0" . $dias[$pos2];
                        //echo $dias[$pos2]; 

                        $w = date('w', mktime(0, 0, 0, date('m'), $dias[$pos2], date('Y')));
                        if($sem == $w && $dias[$pos2] > 0){
                            //echo $dias[$pos2];
                            $horarios[] = '<strong>'.$dias[$pos2].' às '.substr($h['NRT_HOR'],0,5).'</strong>  '.dominio::getTurma($h['CUR_ID'],0); //$h['NRT_NOM'].' - '.$h['CBS_NOM'];
                            $dia[$dias[$pos2]] = $dias[$pos2].' às '.substr($h['NRT_HOR'],0,5).'  '.dominio::getTurma($h['CUR_ID'],0); //$h['NRT_NOM'].' - '.$h['CBS_NOM'];
                        }

                        $cont++;
                    }
                }
            }

            //usort($horarios);
            $this->view->horarios = $horarios;
            $this->view->dia = $dia;
            
            $query = 'SELECT SQL_CACHE *, DAY(CAL_DAT) as DIA, MONTH(CAL_DAT) as MES FROM CALENDARIO WHERE YEAR(CAL_DAT) = "'. date('Y').'" and MONTH(CAL_DAT) = "'. date('m').'" and CAL_STA = 1 ORDER BY CAL_DAT ASC, CAL_NOM ASC LIMIT 18';
            $ves = $this->db->fetchAll($query);
            $this->view->ves = $ves;
            // fim calendario
            */
        }
    }
    
    public function msgAction()
    {  
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender(); 
        $ok = FALSE;
        $id = $this->_getParam('id', 0);
        if($this->getRequest()->isPost()) {
            $this->db->update('MURAL_RECADOS_MSG',array('MRM_STA'=> 2),'MRM_ID = ' . $id);
            $ok = TRUE;
        }
        
        echo json_encode(array('ok' => $ok));
    }

}

